﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using Lswweb.Domain;
using Lswweb.Model;
using Lswweb.Common;

namespace Lswweb.Controls
{
    public class OrderSearch : SkinnedGridView<OrderInfo>
    {
        private TextBox tbxOrderId;
        private TextBox tbxBeginDate;
        private TextBox tbxEndDate;
        private OrderStateDropDownList ddlState;
        private TextBox tbxUserLoginName;

        private IButtonControl btnConfirm;
        private IButtonControl btnToExcel;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            base.InitializeSkin(skin);

            tbxOrderId=(TextBox)skin.FindControl("tbxOrderId");
            tbxBeginDate=(TextBox)skin.FindControl("tbxBeginDate");
            tbxEndDate=(TextBox)skin.FindControl("tbxEndDate");
            tbxUserLoginName = (TextBox)skin.FindControl("tbxUserLoginName");
            ddlState = (OrderStateDropDownList)skin.FindControl("ddlState");
            btnConfirm = (IButtonControl)skin.FindControl("btnConfirm");
            btnToExcel = (IButtonControl)skin.FindControl("btnToExcel");

            btnConfirm.Click += new EventHandler(btnConfirm_Click);
            btnToExcel.Click += new EventHandler(btnToExcel_Click);
        }

        void btnToExcel_Click(object sender, EventArgs e)
        {
            Repeater repeater = this.GridViewControl as Repeater;
            int total;
            repeater.DataSource = GridViewDataSourceHandler(1,999,out total);
            repeater.DataBind();

            ToExcel(repeater, string.Format("{0}.xls",DateTime.Now.ToShortDateString()));
        }

        protected override IList<OrderInfo> GridViewDataSourceHandler(int pageIndex, int pageSize, out int totalRecord)
        {
            if (!string.IsNullOrEmpty(tbxOrderId.Text.Trim()))
            {
                try
                {
                    int OrderId = Convert.ToInt32(tbxOrderId.Text.Trim());
                    OrderInfo order = Orders.GetOrder(OrderId);

                    IList<OrderInfo> list = new List<OrderInfo>();
                    if (order != null)
                        list.Add(order);

                    totalRecord = list.Count;
                    return list;
                }
                catch (FormatException)
                {
                    totalRecord = 0;
                    return new List<OrderInfo>();
                }
            }
            else
            {

                string searchKey = tbxUserLoginName.Text.Trim();
                DateTime? beginDate = string.IsNullOrEmpty(tbxBeginDate.Text.Trim()) ? (DateTime?)null : Convert.ToDateTime(tbxBeginDate.Text.Trim());
                DateTime? endDate = string.IsNullOrEmpty(tbxEndDate.Text.Trim()) ? (DateTime?)null : Convert.ToDateTime(tbxEndDate.Text.Trim());
                OrderStatus? status = Enum.IsDefined(typeof(OrderStatus), ddlState.SelectedValue) ? ddlState.SelectedValue : (OrderStatus?)null;

                return Orders.GetOrders(searchKey, new Pair<DateTime?>(beginDate, endDate), status, pageSize, pageIndex, out totalRecord);
            }
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            this.GridViewDataBind();
        }

        private void ToExcel(Repeater repeater, string FileName)
        {
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">"); ;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);

            repeater.Page.EnableViewState = false;
            StringWriter writer = new System.IO.StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            repeater.RenderControl(htmlWriter);

            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
        }

        protected override string DefaultSkinFilename
        {
            get { return "Order/OrderSearch.ascx"; }
        }
    }
}
